import 'package:better_player/better_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:getx_study/mode/MovieItem.dart';
import 'package:getx_study/video_list/view.dart';

class BetterPlayerPage extends StatefulWidget {
  @override
  _BetterPlayerPageState createState() => _BetterPlayerPageState();
}

class _BetterPlayerPageState extends State<BetterPlayerPage> {

  MovieItem movieItem = Get.arguments;
  late BetterPlayerController _betterPlayerController;

  @override
  void initState() {
    super.initState();
    BetterPlayerDataSource betterPlayerDataSource = BetterPlayerDataSource(
        BetterPlayerDataSourceType.network,
        movieItem.url);
    _betterPlayerController = BetterPlayerController(
        BetterPlayerConfiguration(
          autoPlay: true,
          looping: false,
          fullScreenByDefault: false,
          allowedScreenSleep: false,
          startAt: Duration(seconds: 0),
          controlsConfiguration: BetterPlayerControlsConfiguration(
            playerTheme: BetterPlayerTheme.material
            // textColor: Colors.red,
            // iconsColor: Colors.green,
            // controlBarColor: Colors.pink,
            // progressBarBufferedColor: Colors.yellow,
            // progressBarPlayedColor: Colors.green,
            // progressBarBackgroundColor: Colors.red,
            // progressBarHandleColor: Colors.yellow,
            // progressBarBufferedColor: Colors.blue,
            // loadingColor: Colors.yellow,
          )
        ),
        betterPlayerDataSource: betterPlayerDataSource);

    _betterPlayerController.addEventsListener((event){
      // print("Better player event: ${event.betterPlayerEventType}");
      if (event.betterPlayerEventType == BetterPlayerEventType.hideFullscreen) {
        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ]);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("better video"),),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Get.changeTheme(
            ThemeData(
              platform: TargetPlatform.iOS
            )
          );
        },
      ),
      body: _betterPlayerController != null ?
      AspectRatio(
        aspectRatio: 16 / 9,
        child: BetterPlayer(
          controller: _betterPlayerController,
        ),
      ) :
          Container()
    );
  }
}
